var oper = getQueryString('oper')
var grayId = getQueryString('grayId')
var relationId = getQueryString('id')
var serveName = getQueryString('serveName')
$(function () {
    $('#sysGrayItemId').val(grayId)
    $('#serveName').val(serveName)
    var url = "/admin/dept/tree";
    var options = {
        id: "deptTrees",
        url: url,
        check: {enable: true,chkStyle: "checkbox",
            chkboxType: { "Y": "s", "N": "s" } //该属性设置选中时父子节点互不影响"Y"、"N"的值必须为空
            },
        expandLevel: 2,
    };
    $.tree.init(options);

    var roleAll = JSON.parse(session.get('roleList'))

    roleAll.forEach(function(item){
        $('#roleBox').append('<label class="check-box"><input name="roleId" value="'+item.roleId+'" type="checkbox">'+item.roleName+'</label>')
    })

    $(".check-box:not(.noicheck),.radio-box:not(.noicheck)").each(function () {
        $(this).iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
        })
    })

    $('#oper').val(oper)
    if (oper === 'edit' || oper === 'view') {
        roleDetail(relationId)
        $('#roleCode').attr('readonly', true)
    }

    jQuery.validator.addMethod("format",function(value,element){
        var account = /^[A-Z_]+$/;
        return account.test(value);
    },"请输入大写英文、下划线");

    $.common.initFormInfo();
});


$("#editForm").validate({
    onkeyup: false,
    rules: {
        frontSign: {
            required: true
        },
    },
    messages: {
        frontSign: {
            required: "请输入前端标识"
        },
    },
    focusCleanup: true
});


function roleDetail(id) {
    $.operate.get(api + '/admin/sysfrontgray/' + id, function (res) {
        res.data.oper = oper
        //设置角色反选
        res.data.sysRoles.forEach(function (item) {
            $('input[value='+item.roleId+']').iCheck('check')
        })
        //设置部门反选
        res.data.sysDepts.forEach(function (item) {
            var node = $._tree.getNodeByParam("id", item.deptId)
            $._tree.checkNode(node)
        })
        applyDrawing('#editForm', res.data)

        $.common.initFormInfo();
    }, false)
}


function submitHandler() {
    if ($.validate.form()) {
        var roleIds = $.form.selectCheckeds("roleId")
        var deptIds = $.tree.getCheckedNodes();
        $('#deptIds').val(deptIds);
        $('#roleIds').val(roleIds);
        $.common.ajaxCommitForm($('#editForm'), false);
        return false;
    }
}